var wcid = GetQueryString("wcid");
var tp_id = GetQueryString("tp_id");
//发送请求获取工件信息
var postData;
////
$.ajax({
    url: '/op/allotWorkpiece',
    type: 'post',
    data: {
        "tp_wcid": wcid
    },
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
            // window.location.href = '/projectManage.html';
        }

    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
console.log(postData);
//显示工件信息
var workpieceTable = document.getElementById("workpieceTable");
var workpieceInfomation = [];
var t_id;
for (let i = 0; i < postData.length; i++) {
    workpieceInfomation[i] = [
        postData[i].w_pipeNum,
        postData[i].w_weldNum,
        postData[i].w_pipeName,
        postData[i].w_scale,
        postData[i].w_spec,
        postData[i].w_texture,
        postData[i].w_type,
        postData[i].w_weldM,
        postData[i].w_weldPer,
        postData[i].w_weldTime,
        postData[i].w_test,
        postData[i].w_qua,
        postData[i].w_id
    ];
    t_id = postData[i].w_tid;
}
for (let i = 0; i < workpieceInfomation.length; i++) {
    let tr = document.createElement("tr");
    let inner = "";
    for (let j = 0; j < workpieceInfomation[0].length - 1; j++) {
        inner += "<td>" + workpieceInfomation[i][j] + "</td>";
    }
    inner += "<td><input type=\"checkbox\" onclick=\"OnCheck(this);\"/></td>"
    tr.innerHTML = inner;
    workpieceTable.children[1].appendChild(tr);
}

var staff_id;
var staffName = [];
var checkedWorkpieceID = [];
function OnCheck(element) {
    if (element.checked) {
        checkedWorkpieceID.push(workpieceInfomation[$(element.parentNode.parentNode).index()][12]);
    }
    else {
        checkedWorkpieceID.splice(checkedWorkpieceID.indexOf(workpieceInfomation[$(element.parentNode.parentNode).index()][12]), 1);
    }
}
$.ajax({
    url: '/user/staff',
    type: 'post',
    data: {
        "requestType": "teamList",
        "user_team": user.staff_team
    },
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
            // window.location.href = '/projectManage.html';
        }

    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
for (var i = 0; i < postData.length; i++) {
    staffName.push([postData[i].user_name, postData[i].staff_id]);
}
var teamOption = document.getElementById("groupOption");
for (let i = 0; i < staffName.length; i++) {
    let option = document.createElement("option");
    option.innerHTML = staffName[i][0];
    teamOption.appendChild(option);
}
function OnAllocate() {
    document.getElementById("chooseGroup").setAttribute("class", "modal fade in");
    document.getElementById("chooseGroup").setAttribute("style", "display: block; padding-right: 17px;");
}

function OnCloseGroup(name) {
    document.getElementById(name).setAttribute("class", "modal fade");
    document.getElementById(name).setAttribute("style", "display: none;");
}

function OnConfirmGroup(name) {
    let index = document.getElementById("groupOption").selectedIndex;
    var date = new Date();
    $.ajax({
        url: '/op/allot',
        type: 'post',
        data: {
            "requestType": "leader",
            "d_tid": t_id,
            "d_leader": user.user_id,
            "d_tpid": tp_id,
            "d_uid": staffName[index][1],
            "d_wcid": wcid,
            "d_start": date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
            "w_ids": checkedWorkpieceID
        },
        async: false,
        traditional: true,
        success: function (data, status) {
            if (status == 'success') {
                $.ajax({
                    url: '/op/wp_num',
                    type: 'post',
                    data: {
                        "tp_id": tp_id,
                        "tp_wcid": wcid
                    },
                    async: false,
                    traditional: true,
                    success: function (data, status) {
                        if (status == 'success') {
                        }

                    },
                    error: function (data, status) {
                        if (status == 'error') {
                            alert("你的网络有问题！");
                        }
                    }
                });
                alert("分配成功");
                history.back();
                // window.location.href = '/projectManage.html';
            }

        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    OnCloseGroup(name);
}